unit uViewFactory ;

interface

uses
  Windows,
  uVisitor,
  Contnrs,
  uBasic,
  uObjects,
  uPlacement,
  SysUtils ;

type
  TMedaViewFactory = class( TMedaVisitor )
  private
    fViews : TObjectList ;
  public
    constructor Create ;
    destructor Destroy ; override ;

    function VisitNode( Instance : TMedaObject ) : TObject ; override ;

    function VisitNode( Instance : TMedaBank ) : TObject ; override ;
    function VisitNode( Instance : TMedaPin ) : TObject ; override ;
    function VisitNode( Instance : TMedaWire ) : TObject ; override ;
    function VisitNode( Instance : TMedaLabel ) : TObject ; override ;
    function VisitNode( Instance : TMedaPower ) : TObject ; override ;

    function VisitNode( Instance : TMedaShape ) : TObject ; override ;
    function VisitNode( Instance : TMedaStack ) : TObject ; override ;
    function VisitNode( Instance : TMedaPad ) : TObject ; override ;
    function VisitNode( Instance : TMedaTrack ) : TObject ; override ;
    function VisitNode( Instance : TMedaVia ) : TObject ; override ;
    function VisitNode( Instance : TMedaCopper ) : TObject ; override ;

    function Place( aObject : TMedaObject ) : TMedaView ;
    procedure Unplace( aView : TMedaView ) ;
    function Link( aObject : TMedaObject ; hView : TGUID ) : TMedaView ;
  end ;

var
  ViewFactory       : TMedaViewFactory ;

implementation

uses
  uLayout,
  uViewSchematic,
  uViewPCB ;

{ TMedaViewFactory }

function TMedaViewFactory.Place( aObject : TMedaObject ) : TMedaView ;
var
  iPlace            : TMedaPlace ;
  iPlaceRef         : TGUID ;
begin
  Result := nil ;
  //      raise EVisitor.Create( aObject.ClassName ) ;
  iPlaceRef := ( aObject as TMedaObject ).PlaceRef ;
  if IsEqualGUID( iPlaceRef, nullGUID ) then
    Exit ;

  iPlace := vsbSchematicViewer.PageByID( iPlaceRef ) ;
  if not assigned( iPlace ) then
    iPlace := vsbPCBViewer.LayerByID( iPlaceRef ) ;
  if not assigned( iPlace ) then
    raise ExceptionMeda.Create( 'Place: ' + GUIDToString( iPlaceRef ) + ' not found' ) ;

  Result := TMedaView( aObject.Accept( ViewFactory ) ) ;
  if not assigned( Result ) then
    Exit ;
  Result.Place( iPlace ) ;
  fViews.Add( Result ) ;
end ;

procedure TMedaViewFactory.Unplace( aView : TMedaView ) ;
begin
  //    if assigned( aView.Tree ) and assigned( aView.Node ) then
  //        aView.Tree.DeleteNode( aView.Node, False ) ;
  //    aView.Node := nil ;
  if assigned( aView.Viewer ) then
    ( aView.Viewer as TMedaPlace ).Remove( aView ) ;
  if assigned( aView.Owner ) then
    aView.Owner.View := nil ;
  aView.Owner := nil ;
  fViews.Remove( aView ) ;
end ;

function TMedaViewFactory.Link( aObject : TMedaObject ; hView : TGUID ) : TMedaView ;
var
  i                 : integer ;
begin
  Result := nil ;
  for i := 0 to fViews.Count - 1 do
    if IsEqualGUID( TMedaView( fViews[ i ] ).ID, hView ) then begin
      Result := TMedaView( fViews[ i ] ) ;
      Exit ;
    end ;
end ;

constructor TMedaViewFactory.Create ;
begin
  inherited ;
  fViews := TObjectList.Create( False ) ;
end ;

destructor TMedaViewFactory.Destroy ;
begin
  fViews.Free ;
  inherited ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaLabel ) : TObject ;
begin
  Result := TMSPLabel.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaPower ) : TObject ;
begin
  Result := TMSPPower.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaPin ) : TObject ;
begin
  Result := TMSPPin.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaBank ) : TObject ;
begin
  Result := TMSPBank.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaWire ) : TObject ;
begin
  Result := TMSPWire.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaShape ) : TObject ;
begin
  Result := TMSPShape.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaTrack ) : TObject ;
begin
  Result := TMSPTrack.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaVia ) : TObject ;
begin
  Result := TMSPPad.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaCopper ) : TObject ;
begin
  Result := TMSPCopper.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaStack ) : TObject ;
begin
  Result := nil ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaPad ) : TObject ;
begin
  Result := TMSPPad.Create( Instance ) ;
end ;

function TMedaViewFactory.VisitNode( Instance : TMedaObject ) : TObject ;
begin
  Result := nil ;
  //   raise EVisitor.Create( Instance.ClassName ) ;
end ;

initialization
  ViewFactory := TMedaViewfactory.Create ;

finalization
  ViewFactory.Free ;

end.

